//
//  LoginForgetPwdController.m
//  FinancialStore
//
//  Created by 王晓萌 on 16/8/9.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "LoginForgetPwdController.h"
#import "UIDevice+Additions.h"

@interface LoginForgetPwdController ()<LoginNormalCellDelegate>

@end

@implementation LoginForgetPwdController
#pragma  mark - init
//- (instancetype)init{
//    if (self = [super init]) {
//            }
//    return self;
//}


-(void)viewDidLoad{
    
    [super viewDidLoad];
    self.configureStyle = kLocalConfigForget;
    [self addLeftBackButton];
    [self.loginMainView hiddenExtraViewWith:@"确定"];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

-(void)addLeftBackButton{
   UIButton *backButton = [self addLeftBackButtonForNoneNavi];
    [self.view addSubview:backButton];
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(20);
        make.left.equalTo(self.view.mas_left).offset(10);
        make.size.mas_equalTo(CGSizeMake(40, 60));
    }];
    [backButton addTarget:self action:@selector(backBtnPressed ) forControlEvents:UIControlEventTouchUpInside];
}
- (void)backBtnPressed{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


#pragma mark -
#pragma mark tableview datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    NSArray *array = [LoginMainAction getLocalConfig:self.configureStyle];
    return [array count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LoginNormalCell *cell = [LoginNormalCell loginNormalCell:tableView];
    cell.configDic = [[LoginMainAction getLocalConfig:self.configureStyle] objectAtIndex:indexPath.section];
    cell.userInput.delegate = self;
    cell.loginDelegate = self;

    cell.layoutTouserInput = self.configureStyle;
    
    if (indexPath.section == 1) {
        
            cell.userInput.secureTextEntry = NO;
        
    }else if (indexPath.section == 2 || indexPath.section == 3) {
        
        cell.userInput.secureTextEntry = YES;
        
    }else{
        // to do
    }
    
    [cell.getVerify addTarget:self action:@selector(verifyBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
    
}


//验证码
- (void)verifyBtnPressed:(id)sender {
    NSIndexPath * indexpath = [NSIndexPath indexPathForRow:0 inSection:0];
    LoginNormalCell * cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexpath];
    NSString *phone = cell.userInput.text;
    if (phone.length <= 0) {
        [self showToastAddWithTitle:@"手机号码不能为空"];
        return;
    }
    [LoadingAnimalView showWhiteLoadingAddedTo:self.view];
    [self.loginMainAction forgotWithPhoneNumber:phone withCallBack:^(id error_msg) {
        [LoadingAnimalView hideLoadingForView:self.view];
        if (error_msg) {

            [self showToastAddWithTitle:error_msg];
        }else {
            timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerWithAction) userInfo:nil repeats:YES];
        }
    }];
}

//确定
- (void)loginBtnPressed:(id)sender {
    NSIndexPath * indexpath = [NSIndexPath indexPathForRow:0 inSection:0];
    LoginNormalCell * cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexpath];
    NSString *phone = cell.userInput.text;
    indexpath = [NSIndexPath indexPathForRow:0 inSection:1];
    cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexpath];
    NSString *checksms = cell.userInput.text;
    indexpath = [NSIndexPath indexPathForRow:0 inSection:2];
    cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexpath];
    NSString *passwd = cell.userInput.text;
    indexpath = [NSIndexPath indexPathForRow:0 inSection:3];
    cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexpath];
    NSString *confirmPasswd = cell.userInput.text;
    if (phone.length <= 0) {
        [self showToastAddWithTitle:@"手机号不能为空"];
        return;
    }else if (checksms.length <= 0) {
        [self showToastAddWithTitle:@"验证码不能为空" ];
        return;
    }else if (passwd.length <= 0) {
        [self showToastAddWithTitle:@"密码不能为空"];
        return;
    }else if (confirmPasswd.length <= 0) {
        [self showToastAddWithTitle:@"确认密码不能为空"];
        return;
    }else if (passwd.length < 6 || passwd.length >16) {
        [self showToastAddWithTitle:@"密码长度为6到16个字符"];
        return;
    }else if (![passwd isEqualToString:confirmPasswd]){
        [self showToastAddWithTitle:@"密码和确认密码不等"];
        return;
    }
    
    [LoadingAnimalView showWhiteLoadingAddedTo:self.view];
    [self.loginMainAction forGotForLoginWithPhoneNumber:phone withChecksms:checksms withPasswd:passwd withConfirmPasswd:confirmPasswd withCallBack:^(id error_msg) {
        [LoadingAnimalView hideLoadingForView:self.view];
        if (error_msg) {
            [self showToastAddWithTitle:error_msg];
        }else {
          if (self.isRn) {
//            AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//            [appDelegate.preController.navigationController popViewControllerAnimated:YES];
              UIApplication *application = [UIApplication sharedApplication];
              UIWindow *window = application.keyWindow;
              
              
              
              UIViewController * rootViewC= window.rootViewController;
              [rootViewC.navigationController popViewControllerAnimated:YES];

              
          }else{
            [self.navigationController popViewControllerAnimated:YES];
          }
        }
    }];
}



-(void)hideKeyboard{
    
    [self.view endEditing:YES];
    isEditing = NO;
    if([UIDevice isiPhone4] || [UIDevice isiPhone5]){
        CGRect frame = self.view.frame;
        [self.view setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        
    }

    
}

#pragma mark -
#pragma mark textfield delegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2];
        LoginNormalCell *cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
        UITextField * enterPassWord = cell.userInput;
        indexPath = [NSIndexPath indexPathForRow:0 inSection:3];
        cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
        UITextField * confirmPassWord = cell.userInput;

        if (textField == enterPassWord) {
            if ([confirmPassWord.text isEqualToString:@""]||(range.location == 0 && [string isEqualToString:@""])) {
                [self.loginMainView initLoginBtn];
            }else{
                [self.loginMainView activeLoginBtn];
            }
        }else if(textField == confirmPassWord) {
            if ([enterPassWord.text isEqualToString:@""]||(range.location == 0 && [string isEqualToString:@""])) {
                
                [self.loginMainView initLoginBtn];
            }else{
                [self.loginMainView activeLoginBtn];
            }
                
        }else{
            
        }
    return  YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    
    if([UIDevice isiPhone4]){
        
        CGRect frame = self.view.frame;
        [self.view setFrame:CGRectMake(0, -88, frame.size.width, frame.size.height)];
        
    }
    
    if([UIDevice isiPhone5]){
        
        CGRect frame = self.view.frame;
        [self.view setFrame:CGRectMake(0, -44, frame.size.width, frame.size.height)];
    }
    
    isEditing = YES;
    
    
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    
    if(isEditing){
        
        isEditing = NO;
        return YES;
    }
    
    if([UIDevice isiPhone4] || [UIDevice isiPhone5]){
        CGRect frame = self.view.frame;
        [self.view setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        
    }
    return YES;
}
- (void)clearContent:(LoginNormalCell *)cell{
    [loginMainView initLoginBtn];
}


@end
